#include "cellmainwindow.h"
#include <QtWidgets>

CellMainWindow::CellMainWindow()
{
    undoStack = new QUndoStack(this);

    this->setWindowTitle(tr("Cell"));
    this->setPalette(QPalette(QColor(68, 68, 68)));
    this->resize(1280, 700);
    this->setMinimumSize(1280, 700);
    centralWidget = new QWidget;
    setCentralWidget(centralWidget);

    createMenuActions();
    createMenus();
    createFileToolBar();
    createOpenGLWindow();
    createRightClickMenu();
    createLeftPart();
    createRightPart();
    createStatusBar();

    QWidget *blankWidget = new QWidget;
    blankWidget->setFixedHeight(10);

    centerLayout = new QGridLayout;
    centerLayout->addWidget(leftPart, 0, 0);
    centerLayout->addWidget(perspectiveWidgetArea, 0, 1);
    centerLayout->addWidget(rightPart, 0, 2);
    centerLayout->addWidget(blankWidget, 1, 1);
    centralWidget->setLayout(centerLayout);

    //to avoid a segfault in cases where recCommand is used but has not been allocated yet
    recCommand = NULL;
}

CellMainWindow::~CellMainWindow()
{
    //cout << "CellMainWindow::~CellMainWindow()" << endl;
}

bool CellMainWindow::eventFilter(QObject *target, QEvent *event)
{
    if (target==editMeshDockWidget && event->type()==QEvent::Close) {
        perspectiveWidget->setSelectMode(CELL_SELECT);
        perspectiveWidget->updateGL();
        return true;
    }
    return QMainWindow::eventFilter(target, event);
}

void CellMainWindow::createStatusBar()
{
    statusBar()->showMessage(tr("Welcome"), 2000);
}

void CellMainWindow::createOpenGLWindow()
{
    perspectiveWidget = new GLWidget;
    perspectiveWidgetArea = new QScrollArea;
    perspectiveWidgetArea->setWidget(perspectiveWidget);
    perspectiveWidgetArea->setWidgetResizable(true);
    perspectiveWidgetArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    perspectiveWidgetArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    perspectiveWidgetArea->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    perspectiveWidgetArea->setStyleSheet(QScrollAreaStyle());
}

void CellMainWindow::createMenuActions()
{
    newSceneAction = new QAction(tr("New Scene"), this);
    newSceneAction->setShortcut(tr("Ctrl+N"));
    connect(newSceneAction, SIGNAL(triggered()), this, SLOT(newScene()));
    importXMLAction = new QAction(tr("Import XML..."), this);
    connect(importXMLAction, SIGNAL(triggered()), this, SLOT(importXML()));
    importSBMLAction = new QAction(tr("Import SBML..."), this);
    connect(importSBMLAction, SIGNAL(triggered()), this, SLOT(importSBML()));
    saveAction = new QAction(tr("Save..."), this);
    saveAction->setShortcut(tr("Ctrl+S"));
    connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));

    undoAction = undoStack->createUndoAction(this, tr("&Undo"));
    undoAction->setShortcuts(QKeySequence::Undo);

    redoAction = undoStack->createRedoAction(this, tr("&Redo"));
    redoAction->setShortcuts(QKeySequence::Redo);

    cutAction = new QAction(tr("Cut"), this);
    cutAction->setShortcut(tr("Ctrl+X"));
    connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
    copyAction = new QAction(tr("Copy"), this);
    copyAction->setShortcut(tr("Ctrl+C"));
    connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
    pasteAction = new QAction(tr("Paste"), this);
    pasteAction->setShortcut(tr("Ctrl+V"));
    connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
    selectAction = new QAction(tr("Select"), this);
    connect(selectAction, SIGNAL(triggered()), this, SLOT(select()));

    surfaceConstructionAction = new QAction(tr("Surface Construction"), this);
    connect(surfaceConstructionAction, SIGNAL(triggered()), this,
            SLOT(surfaceConstructionSlot()));
    editMeshAction = new QAction(tr("Edit Mesh"), this);
    connect(editMeshAction, SIGNAL(triggered()), this,
            SLOT(editMeshSlot()));

    unionAction = new QAction(tr("Union"), this);
    connect(unionAction, SIGNAL(triggered()), this,
            SLOT(unionSlot()));
    subtractAction = new QAction(tr("Subtract"), this);
    connect(subtractAction, SIGNAL(triggered()), this,
            SLOT(subtractSlot()));
    intersectAction = new QAction(tr("Intersect"), this);
    connect(intersectAction, SIGNAL(triggered()), this,
            SLOT(intersectSlot()));
}

void CellMainWindow::editMeshSlot()
{
    if (perspectiveWidget->selectedObject()>=0) {
        createMeshEditWidget();
        perspectiveWidget->updateGL();
    } else {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::critical(this, tr("QMessageBox::critical()"),
                                        tr("Please select a object."),
                                        QMessageBox::Abort | QMessageBox::Retry);
        if (reply == QMessageBox::Retry)
            editMeshSlot();
    }
}

void CellMainWindow::surfaceConstructionSlot()
{
    if (perspectiveWidget->selectedObject()>=0) {
        if (perspectiveWidget->getGlandular()->isSurfaceConstructed()==false) {
            bool ok;
            double radius = QInputDialog::getDouble(this, tr("Surface Construction"),
                                               tr("Pivot Ball Radius:"), 0.0, 0.0, 10000, 6, &ok);
            if (ok) {
                perspectiveWidget->getGlandular()->SurfaceReconstruction(radius);
                perspectiveWidget->updateGL();
            }
        } else {
            QMessageBox::StandardButton reply;
            reply = QMessageBox::critical(this, tr("QMessageBox::critical()"),
                                            tr("This object has faces."),
                                            QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore);
            if (reply == QMessageBox::Retry)
                surfaceConstructionSlot();
        }
    } else {
        QMessageBox::StandardButton reply;
        reply = QMessageBox::critical(this, tr("QMessageBox::critical()"),
                                        tr("Please select an object."),
                                        QMessageBox::Abort | QMessageBox::Retry);
        if (reply == QMessageBox::Retry)
            surfaceConstructionSlot();
    }
}

/*File: include “new scene”, “import XML”, “import SBML”, “save”
Edit: include “cut”, “copy”, “paste”, “undo”, “redo”, “select (select objects by object names or IDs, it will appear a new dialog box)”
Display: include “show (grids, points, lines, triangles)”, “view (perspective, top, right, front, 4, plane base)”
Simluation:
Plugins: include all the plug-ins
Help: F1 */
void CellMainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(newSceneAction);
    fileMenu->addAction(importXMLAction);
    fileMenu->addAction(importSBMLAction);
    fileMenu->addAction(saveAction);

    editMenu = menuBar()->addMenu(tr("&Edit"));
    editMenu->addAction(undoAction);
    editMenu->addAction(redoAction);
    editMenu->addSeparator();
    editMenu->addAction(cutAction);
    editMenu->addAction(copyAction);
    editMenu->addAction(pasteAction);
    editMenu->addSeparator();
    editMenu->addAction(selectAction);

    displayMenu = menuBar()->addMenu(tr("&Display"));

    simulationMenu = menuBar()->addMenu(tr("&Simulation"));

    pluginsMenu = menuBar()->addMenu(tr("&Plugins"));
    pluginsMenu->addAction(surfaceConstructionAction);
    pluginsMenu->addAction(editMeshAction);

    booleanMenu = pluginsMenu->addMenu(tr("&Boolean"));
    booleanMenu->addAction(unionAction);
    booleanMenu->addAction(subtractAction);
    booleanMenu->addAction(intersectAction);

    helpMenu = menuBar()->addMenu(tr("&Help"));

}

void CellMainWindow::createRightClickMenu()
{
    cutActionRC = new QAction(tr("Cut"), this);
    connect(cutActionRC, SIGNAL(triggered()), this, SLOT(cut()) );
    copyActionRC = new QAction(tr("Copy"), this);
    connect(copyActionRC, SIGNAL(triggered()), this, SLOT(copy()) );
    pasteActionRC = new QAction(tr("Paste"), this);
    connect(pasteActionRC, SIGNAL(triggered()), this, SLOT(paste()) );
    editMeshActionRC = new QAction(tr("Edit Mesh"), this);
    connect(editMeshActionRC, SIGNAL(triggered()), this, SLOT(editMeshSlot()) );
    deleteActionRC = new QAction(tr("Delete"), this);
    connect(deleteActionRC, SIGNAL(triggered()), this, SLOT(deleteSelectedObjectSlot()) );
    generateMeshActionRC = new QAction(tr("Generate Mesh"), this);
    connect(generateMeshActionRC, SIGNAL(triggered()), this, SLOT(generateMeshSlot()) );

    rightClickMenu = new QMenu(this);
    rightClickMenu->addAction(cutActionRC);
    rightClickMenu->addAction(copyActionRC);
    rightClickMenu->addAction(pasteActionRC);
    rightClickMenu->addAction(editMeshActionRC);
    rightClickMenu->addAction(deleteActionRC);
    rightClickMenu->addAction(generateMeshActionRC);
    connect(perspectiveWidget, SIGNAL(rightClickSelect(QPoint)),
            this, SLOT(rightClickMenuSlot(QPoint)) );
}

//left part widget contains transform tool group, view tool group, camera tool group, dispaly mode tool group
void CellMainWindow::createLeftPart()
{
    leftPart = new QWidget;
    createTransformToolGroup();
    createViewToolGroup();
    createCameraToolGroup();
    createDisplayModeToolGroup();
    leftPart->setFixedWidth(84);
}


void CellMainWindow::createTransformToolGroup()
{
    transformToolWidget = new QWidget(leftPart);
    transformToolGroup = new QButtonGroup(transformToolWidget);
    connect(transformToolGroup, SIGNAL(buttonClicked(QAbstractButton*)),
                this, SLOT(transformToolGroupClicked(QAbstractButton*)));
    createToolButton(transformToolWidget, transformToolGroup, selectButton,
                     tr("Select"), tr(":/images/select"), 0, 0, 40, 40, true);
    createToolButton(transformToolWidget, transformToolGroup, translateButton,
                     tr("Translate"), tr(":/images/translate"), 0, 42, 40, 40, false);
    /*
    createToolButton(transformToolWidget, transformToolGroup, rotateButton,
                     tr("Rotate"), tr(":/images/rotate"), 0, 84, 40, 40, false);
    createToolButton(transformToolWidget, transformToolGroup, scaleButton,
                     tr("Scale"), tr(":/images/scale"), 0, 126, 40, 40, false);
    createToolButton(transformToolWidget, transformToolGroup, ctrlPointsButton,
                     tr("Control Points"), tr(":/images/paint6"), 0, 168, 40, 40, false);
                     */

    transformToolWidget->setGeometry(0,0,40,208);
}

void CellMainWindow::transformToolGroupClicked(QAbstractButton *button)
{
    QList<QAbstractButton *> buttons = transformToolGroup->buttons();
    foreach (QAbstractButton *myButton, buttons) {
        if (myButton != button)
        button->setChecked(false);
    }
    QString text = button->objectName();
    if (text == tr("Select")) {
        perspectiveWidget->setMode(CELL_SELECT);
    } else if (text == tr("Translate")) {
        perspectiveWidget->setMode(CELL_TRANSLATE);
    } else if (text == tr("Rotate")) {
        perspectiveWidget->setMode(CELL_ROTATE);
    } else if (text == tr("Scale")) {
        perspectiveWidget->setMode(CELL_SCALE);
    } else if (text == tr("CameraMove")) {
        perspectiveWidget->setMode(CELL_CAMERAMOVE);
    } else if (text == tr("CameraRotate")) {
        perspectiveWidget->setMode(CELL_CAMERAROTATE);
    } else if (text == tr("CameraZoom")) {
        perspectiveWidget->setMode(CELL_CAMERAZOOM);
    } else {
        perspectiveWidget->setMode(CELL_STOP);
    }
    perspectiveWidget->updateGL();
}

void CellMainWindow::createViewToolGroup()
{
    viewToolWidget = new QWidget(leftPart);
    viewToolGroup = new QButtonGroup(this);
    connect(viewToolGroup, SIGNAL(buttonClicked(QAbstractButton*)),
                this, SLOT(viewToolGroupClicked(QAbstractButton*)));
    createToolButton(viewToolWidget, viewToolGroup, perspectiveViewButton,
                     tr("Perspective View"), tr(":/images/perspective"), 0, 0, 40, 40, true);
    createToolButton(viewToolWidget, viewToolGroup, fourViewButton,
                     tr("Four View"), tr(":/images/four"), 0, 42, 40, 40, false);
    createToolButton(viewToolWidget, viewToolGroup, topViewButton,
                     tr("Top View"), tr(":/images/top"), 0, 84, 40, 40, false);
    createToolButton(viewToolWidget, viewToolGroup, rightViewButton,
                     tr("Right View"), tr(":/images/right"), 0, 126, 40, 40, false);
    createToolButton(viewToolWidget, viewToolGroup, frontViewButton,
                     tr("Front View"), tr(":/images/front"), 0, 168, 40, 40, false);
    createToolButton(viewToolWidget, viewToolGroup, planeBaseViewButton,
                     tr("Plane Base View"), tr(":/images/plane"), 0, 210, 40, 40, false);

    resetViewButton = new QPushButton(viewToolWidget);
    connect(resetViewButton, SIGNAL(pressed()), this, SLOT(resetButtonPressed()));
    resetViewButton->setGeometry(0, 252, 40, 40);
    resetViewButton->setObjectName(tr("Reset View"));
    resetViewButton->setStyleSheet(PushButtonStyle(tr(":/images/reset")));
    resetViewButton->setFixedSize(40, 40);
    resetViewButton->setCheckable(true);
    resetViewButton->setStatusTip(tr("Reset View"));
    resetViewButton->setToolTip("Reset View");

    viewToolWidget->setGeometry(0, 226, 40, 292);
}

void CellMainWindow::resetButtonPressed()
{
    perspectiveWidget->reset();
    perspectiveWidget->updateGL();
}

void CellMainWindow::viewToolGroupClicked(QAbstractButton *button)
{
    QList<QAbstractButton *> buttons = transformToolGroup->buttons();
    foreach (QAbstractButton *myButton, buttons) {
        if (myButton != button)
        button->setChecked(false);
    }
    QString text = button->objectName();
    if (text == tr("Perspective View")) {
        perspectiveWidget->setViewMode(CELL_PERSPECTIVE);
    } else if (text == tr("Four View")) {
        perspectiveWidget->setViewMode(CELL_FOURVIEW);
    } else if (text == tr("Top View")) {
        perspectiveWidget->setViewMode(CELL_TOP);
    } else if (text == tr("Right View")) {
        perspectiveWidget->setViewMode(CELL_RIGHT);
    } else if (text == tr("Front View")) {
        perspectiveWidget->setViewMode(CELL_FRONT);
    } else if (text == tr("Plane Base View")) {
        perspectiveWidget->setViewMode(CELL_PALNEBACED);
        perspectiveWidget->getGlandular()->ComputeIntersectionPlane(0.0, 1.0, 0.0, perspectiveWidget->plane->translateY);
    }

    perspectiveWidget->updateGL();
}

void CellMainWindow::createCameraToolGroup()
{
    cameraToolWidget = new QWidget(leftPart);
//    cameraToolGroup = new QButtonGroup(this);
//    connect(cameraToolGroup, SIGNAL(buttonClicked(QAbstractButton*)),
//                this, SLOT(cameraToolGroupClicked(QAbstractButton*)));
    createToolButton(cameraToolWidget, transformToolGroup, cameraMoveButton,
                     tr("CameraMove"), tr(":/images/cameraMove"), 0, 0, 40, 40, false);
    createToolButton(cameraToolWidget, transformToolGroup, cameraRotateButton,
                     tr("CameraRotate"), tr(":/images/cameraRotate"), 0, 42, 40, 40, false);
    createToolButton(cameraToolWidget, transformToolGroup, cameraZoomButton,
                     tr("CameraZoom"), tr(":/images/cameraZoom"), 0, 84, 40, 40, false);

    cameraToolWidget->setGeometry(0, 84, 40, 124);
}

void CellMainWindow::cameraToolGroupClicked(QAbstractButton *button)
{
    QList<QAbstractButton *> buttons = cameraToolGroup->buttons();
    foreach (QAbstractButton *myButton, buttons) {
        if (myButton != button)
        button->setChecked(false);
    }
    QString text = button->text();
}

void CellMainWindow::createDisplayModeToolGroup()
{
    dispalyModeToolGroup = new QButtonGroup(this);
    connect(dispalyModeToolGroup, SIGNAL(buttonClicked(QAbstractButton*)),
                this, SLOT(disolayModeToolGroupClicked(QAbstractButton*)));
    displayModeToolWidget = new QWidget(leftPart);
    createToolButton(displayModeToolWidget, dispalyModeToolGroup, pointButton,
                     tr("Point"), tr(":/images/point"), 0, 0, 40, 40, false);
    createToolButton(displayModeToolWidget, dispalyModeToolGroup, lineButton,
                     tr("Line"), tr(":/images/line"), 0, 42, 40, 40, false);
    createToolButton(displayModeToolWidget, dispalyModeToolGroup, triangleButton,
                     tr("Triangle"), tr(":/images/triangle"), 0, 84, 40, 40, true);

    displayModeToolWidget->setGeometry(42, 226, 40, 124);
}

void CellMainWindow::disolayModeToolGroupClicked(QAbstractButton *button)
{
    QList<QAbstractButton *> buttons = transformToolGroup->buttons();
    foreach (QAbstractButton *myButton, buttons) {
        if (myButton != button)
        button->setChecked(false);
    }
    QString text = button->objectName();
    if (text == tr("Point")) {
        perspectiveWidget->setDisplayMode(CELL_POINT);
    } else if (text == tr("Line")) {
        perspectiveWidget->setDisplayMode(CELL_LINE);
    } else if (text == tr("Triangle")) {
        perspectiveWidget->setDisplayMode(CELL_TRIANGLE);
    }

    perspectiveWidget->updateGL();
}


void CellMainWindow::createToolButton(QWidget *widget, QButtonGroup *buttonGroup, QToolButton *button,
                                          const QString &text, const QString &image,
                                          const int x, const int y, const int w, const int h, bool checked)
{
    button = new QToolButton(widget);
    button->setGeometry(x, y, w, h);
    button->setObjectName(text);
    button->setStyleSheet(ToolButtonStyle(image));
    button->setFixedSize(w, h);
    button->setCheckable(true);
    button->setStatusTip(text);
    button->setToolTip(text);
    button->setChecked(checked);

    buttonGroup->addButton(button);
}

void CellMainWindow::createToolButton1(QWidget *widget, QToolButton *button,
                                       const QString &text, const QString &image,
                                       const int x, const int y, const int w, const int h, bool checked)
{
    button = new QToolButton(widget);
    button->setGeometry(x, y, w, h);
    button->setObjectName(text);
    button->setStyleSheet(ToolButtonStyle(image));
    button->setFixedSize(w, h);
    button->setStatusTip(text);
    button->setCheckable(true);
    button->setChecked(checked);
}

void CellMainWindow::createFileToolBar()
{
    fileToolBar = addToolBar(tr("File & Edit"));
    fileToolBar->setMovable(false);
    newSceneActionTB = new QAction(QIcon(":/images/newSceneImg.png"), tr("NewScene"), this);
    newSceneActionTB->setStatusTip(tr("Create a new scene"));
    connect(newSceneActionTB, SIGNAL(triggered()), this, SLOT(newScene()));
    importXMLActionTB = new QAction(QIcon(":/images/importXMLImg.png"), tr("ImportXML"), this);
    importXMLActionTB->setStatusTip(tr("Import XML file"));
    connect(importXMLActionTB, SIGNAL(triggered()), this, SLOT(importXML()));
    importSBMLActionTB = new QAction(QIcon(":/images/importSBMLImg.png"), tr("ImportSBML"), this);
    importSBMLActionTB->setStatusTip(tr("Import SBML file"));
    connect(importSBMLActionTB, SIGNAL(triggered()), this, SLOT(importSBML()));
    saveActionTB = new QAction(QIcon(":/images/saveImg.png"), tr("Save"), this);
    saveActionTB->setStatusTip(tr("Save file"));
    connect(saveActionTB, SIGNAL(triggered()), this, SLOT(save()));

    fileToolBar->addAction(newSceneActionTB);
    fileToolBar->addAction(importXMLActionTB);
    fileToolBar->addAction(importSBMLActionTB);
    fileToolBar->addAction(saveActionTB);
    fileToolBar->addSeparator();

    cutActionTB = new QAction(QIcon(":/images/cutImg.png"), tr("Cut"), this);
    cutActionTB->setStatusTip(tr("Cut"));
    connect(cutActionTB, SIGNAL(triggered()), this, SLOT(cut()));
    copyActionTB = new QAction(QIcon(":/images/copyImg.png"), tr("Copy"), this);
    copyActionTB->setStatusTip(tr("Copy"));
    connect(copyActionTB, SIGNAL(triggered()), this, SLOT(copy()));
    pasteActionTB = new QAction(QIcon(":/images/pasteImg.png"), tr("Paste"), this);
    pasteActionTB->setStatusTip(tr("Paste"));
    connect(pasteActionTB, SIGNAL(triggered()), this, SLOT(paste()));
    undoActionTB = new QAction(QIcon(":/images/undoImg.png"), tr("Undo"), this);
    undoActionTB->setStatusTip(tr("Undo"));
    connect(undoActionTB, SIGNAL(triggered()), undoStack, SLOT(undo()));
    redoActionTB = new QAction(QIcon(":/images/redoImg.png"), tr("Redo"), this);
    redoActionTB->setStatusTip(tr("Redo"));
    connect(redoActionTB, SIGNAL(triggered()), undoStack, SLOT(redo()));

    fileToolBar->addAction(cutActionTB);
    fileToolBar->addAction(copyActionTB);
    fileToolBar->addAction(pasteActionTB);
    fileToolBar->addAction(undoActionTB);
    fileToolBar->addAction(redoActionTB);
    fileToolBar->setAllowedAreas(Qt::TopToolBarArea);
    fileToolBar->setFixedHeight(35);
    //fileToolBar->setStyleSheet(ToolBarStyle());
}

//right part widget contains object group, imformation group
void CellMainWindow::createRightPart()
{
    rightPart = new QWidget;
    createObjectTabWidget();
    createImformationWidget();
    rightPart->setFixedWidth(200);
}

void CellMainWindow::inputSlot(int type)
{
    if (type==CELL_NONE) {
        sphereInputWidget->setVisible(false);
        cylinderInputWidget->setVisible(false);
        planeInputWidget->setVisible(false);
        cubeInputWidget->setVisible(false);
    } else if (type==CELL_MESH) {
        sphereInputWidget->setVisible(false);
        cylinderInputWidget->setVisible(false);
        planeInputWidget->setVisible(false);
        cubeInputWidget->setVisible(false);
    } else if (type==CELL_SPHERE) {
        sphereInputWidget->setVisible(true);
        cylinderInputWidget->setVisible(false);
        planeInputWidget->setVisible(false);
        cubeInputWidget->setVisible(false);
    } else if (type==CELL_CYLINDER||type==CELL_OPENCYLINDER) {
        sphereInputWidget->setVisible(false);
        cylinderInputWidget->setVisible(true);
        planeInputWidget->setVisible(false);
        cubeInputWidget->setVisible(false);
    } else if (type==CELL_PLANE) {
        sphereInputWidget->setVisible(false);
        cylinderInputWidget->setVisible(false);
        planeInputWidget->setVisible(true);
        cubeInputWidget->setVisible(false);
    } else if (type==CELL_CUBE) {
        sphereInputWidget->setVisible(false);
        cylinderInputWidget->setVisible(false);
        planeInputWidget->setVisible(false);
        cubeInputWidget->setVisible(true);
    } else if (type==CELL_LINE) {
        sphereInputWidget->setVisible(false);
        cylinderInputWidget->setVisible(false);
        planeInputWidget->setVisible(false);
        cubeInputWidget->setVisible(false);
        //TODO
    }
}



void CellMainWindow::createObjectTabWidget()
{
    objectToolWidget = new QWidget(rightPart);
    objectToolBox = new QToolBox(objectToolWidget);
    cellWidget = new QWidget;
    basementMembraneWidget = new QWidget;
    bloodVesselWidget = new QWidget;
    ECMWidget = new QWidget;
    inputWidget = new QWidget;

    //Cell QWidget
    QLabel *celllabel1 = new QLabel(tr("Cell Line"), cellWidget);
    QLabel *celllabel2 = new QLabel(tr("DCIS - patient 19"), cellWidget);
    QLabel *celllabel3 = new QLabel(tr("Human dermal fibrolas\n   (ATTC PCS-201-012)"), cellWidget);
    QLabel *celllabel4 = new QLabel(tr("Neutrophils - patient 17"), cellWidget);
    celllabel1->setGeometry(4,0,200,15);
    celllabel2->setGeometry(4,17,200,15);
    celllabel3->setGeometry(4,34,200,30);
    celllabel4->setGeometry(4,68,200,15);

    QLabel *celllabel5 = new QLabel(tr("Edit phenotype"), cellWidget);
    QLabel *celllabel6 = new QLabel(tr("Diameter:"), cellWidget);
    QLabel *celllabel7 = new QLabel(tr("Confluence:"), cellWidget);
    celllabel5->setGeometry(4,100,100,15);
    celllabel6->setGeometry(4,117,100,15);
    celllabel7->setGeometry(4,134,100,15);

    celllabel1->setStyleSheet(LabelStyle());
    celllabel2->setStyleSheet(LabelStyle());
    celllabel3->setStyleSheet(LabelStyle());
    celllabel4->setStyleSheet(LabelStyle());
    celllabel5->setStyleSheet(LabelStyle());
    celllabel6->setStyleSheet(LabelStyle());
    celllabel7->setStyleSheet(LabelStyle());

    QLineEdit *celledit1 = new QLineEdit(tr("19.2 um"), cellWidget);
    QLineEdit *celledit2 = new QLineEdit(tr("85%"), cellWidget);
    celledit1->setGeometry(100,117,100,15);
    celledit2->setGeometry(100,134,100,15);
    celledit1->setStyleSheet(LineEditStyle());
    celledit2->setStyleSheet(LineEditStyle());

    //Basement Membrane QWidget
    QLabel *basementMembranelabel1 = new QLabel(tr("Type"), basementMembraneWidget);
    QLabel *basementMembranelabel2 = new QLabel(tr("Breast BM Type 1"), basementMembraneWidget);
    QLabel *basementMembranelabel3 = new QLabel(tr("Cirrhotic liver - \n   Patient 7 (HepC)"), basementMembraneWidget);
    QLabel *basementMembranelabel4 = new QLabel(tr("Bioengineered 7"), basementMembraneWidget);
    basementMembranelabel1->setGeometry(4,0,200,15);
    basementMembranelabel2->setGeometry(4,17,200,15);
    basementMembranelabel3->setGeometry(4,34,200,30);
    basementMembranelabel4->setGeometry(4,68,200,15);
    QLabel *basementMembranelabel5 = new QLabel(tr("Edit full phenotype"), basementMembraneWidget);
    QLabel *basementMembranelabel6 = new QLabel(tr("Thickness:"), basementMembraneWidget);
    QLabel *basementMembranelabel7 = new QLabel(tr("Young's modulus:"), basementMembraneWidget);
    basementMembranelabel5->setGeometry(4,100,180,15);
    basementMembranelabel6->setGeometry(4,117,100,15);
    basementMembranelabel7->setGeometry(4,134,120,15);
    basementMembranelabel1->setStyleSheet(LabelStyle());
    basementMembranelabel2->setStyleSheet(LabelStyle());
    basementMembranelabel3->setStyleSheet(LabelStyle());
    basementMembranelabel4->setStyleSheet(LabelStyle());
    basementMembranelabel5->setStyleSheet(LabelStyle());
    basementMembranelabel6->setStyleSheet(LabelStyle());
    basementMembranelabel7->setStyleSheet(LabelStyle());
    QLineEdit *basementMembraneedit1 = new QLineEdit(tr("100 nm"), basementMembraneWidget);
    QLineEdit *basementMembraneedit2 = new QLineEdit(tr("450 Pa"), basementMembraneWidget);
    basementMembraneedit1->setGeometry(130,117,70,15);
    basementMembraneedit2->setGeometry(130,134,70,15);
    basementMembraneedit1->setStyleSheet(LineEditStyle());
    basementMembraneedit2->setStyleSheet(LineEditStyle());
    //Create PaintToolGroup inside Basement Membrane Tab Widget
    paintToolWidget = new QWidget(basementMembraneWidget);
    paintToolGroup = new QButtonGroup(basementMembraneWidget);
    paintToolGroup->setExclusive(false);
    connect(paintToolGroup, SIGNAL(buttonPressed(QAbstractButton*)),
                this, SLOT(paintToolGroupPressed(QAbstractButton*)));
    connect(paintToolGroup, SIGNAL(buttonReleased(QAbstractButton*)),
                this, SLOT(paintToolGroupReleased(QAbstractButton*)));
    createToolButton(paintToolWidget, paintToolGroup, paintButton1,
                     tr("Sphere"), tr(":/images/addSphere"), 0, 0, 32, 32, false);
    createToolButton(paintToolWidget, paintToolGroup, paintButton2,
                     tr("Line"), tr(":/images/addClosedCylinder"), 34, 0, 32, 32, false);
    /*
    createToolButton(paintToolWidget, paintToolGroup, paintButton2,
                     tr("Closed Cylinder"), tr(":/images/addClosedCylinder"), 34, 0, 32, 32, false);
    createToolButton(paintToolWidget, paintToolGroup, paintButton3,
                     tr("Cube"), tr(":/images/addCube"), 68, 0, 32, 32, false);
    createToolButton(paintToolWidget, paintToolGroup, paintButton4,
                     tr("Open Cylinder"), tr(":/images/addOpenCylinder"), 102, 0, 32, 32, false);
    createToolButton(paintToolWidget, paintToolGroup, paintButton5,
                     tr("Plane"), tr(":/images/addPlane"), 136, 0, 32, 32, false);
    createToolButton(paintToolWidget, paintToolGroup, paintButton6,
                     tr("Line"), tr(":/images/addSphere"), 170, 0, 32, 32, false);
                     */

    paintToolWidget->setGeometry(4, 161, 400, 40);

    //Blood Vessel QWidget
    QLabel *bloodVessellabel1 = new QLabel(tr("Type"), bloodVesselWidget);
    QLabel *bloodVessellabel2 = new QLabel(tr("Line source (2D/3D)"), bloodVesselWidget);
    QLabel *bloodVessellabel3 = new QLabel(tr("Cross-sectional source"), bloodVesselWidget);
    bloodVessellabel1->setGeometry(4,0,200,15);
    bloodVessellabel2->setGeometry(4,17,200,15);
    bloodVessellabel3->setGeometry(4,34,200,30);
    QLabel *bloodVessellabel4 = new QLabel(tr("Edit full phenotype"), bloodVesselWidget);
    QLabel *bloodVessellabel5 = new QLabel(tr("Diameter:"), bloodVesselWidget);
    QLabel *bloodVessellabel6 = new QLabel(tr("Porosity:"), bloodVesselWidget);
    QLabel *bloodVessellabel7 = new QLabel(tr("Young's modulus:"), bloodVesselWidget);
    QLabel *bloodVessellabel8 = new QLabel(tr("Source properties"), bloodVesselWidget);
    bloodVessellabel4->setGeometry(4,66,180,15);
    bloodVessellabel5->setGeometry(4,83,100,15);
    bloodVessellabel6->setGeometry(4,100,100,15);
    bloodVessellabel7->setGeometry(4,117,120,15);
    bloodVessellabel8->setGeometry(4,134,100,15);
    bloodVessellabel1->setStyleSheet(LabelStyle());
    bloodVessellabel2->setStyleSheet(LabelStyle());
    bloodVessellabel3->setStyleSheet(LabelStyle());
    bloodVessellabel4->setStyleSheet(LabelStyle());
    bloodVessellabel5->setStyleSheet(LabelStyle());
    bloodVessellabel6->setStyleSheet(LabelStyle());
    bloodVessellabel7->setStyleSheet(LabelStyle());
    bloodVessellabel8->setStyleSheet(LabelStyle());
    QLineEdit *bloodVesseledit1 = new QLineEdit(tr("12 um"), bloodVesselWidget);
    QLineEdit *bloodVesseledit2 = new QLineEdit(tr("3%"), bloodVesselWidget);
    QLineEdit *bloodVesseledit3 = new QLineEdit(tr("500 Pa"), bloodVesselWidget);
    bloodVesseledit1->setGeometry(130,83,70,15);
    bloodVesseledit2->setGeometry(130,100,70,15);
    bloodVesseledit3->setGeometry(130,117,70,15);
    bloodVesseledit1->setStyleSheet(LineEditStyle());
    bloodVesseledit2->setStyleSheet(LineEditStyle());
    bloodVesseledit3->setStyleSheet(LineEditStyle());

    //ECM QWidget
    QLabel *ECMlabel1 = new QLabel(tr("Type"), ECMWidget);
    QLabel *ECMlabel2 = new QLabel(tr("Breast stroma 3"), ECMWidget);
    QLabel *ECMlabel3 = new QLabel(tr("Smoker's lung' - \n   Patient 7"), ECMWidget);
    QLabel *ECMlabel4 = new QLabel(tr("Human liver - explant 1"), ECMWidget);
    ECMlabel1->setGeometry(4,0,200,15);
    ECMlabel2->setGeometry(4,17,200,15);
    ECMlabel3->setGeometry(4,34,200,30);
    ECMlabel4->setGeometry(4,68,200,15);
    QLabel *ECMlabel5 = new QLabel(tr("Edit full phenotype"), ECMWidget);
    QLabel *ECMlabel6 = new QLabel(tr("Volume fraction:"), ECMWidget);
    QLabel *ECMlabel7 = new QLabel(tr("Young's modulus:"), ECMWidget);
    ECMlabel5->setGeometry(4,100,180,15);
    ECMlabel6->setGeometry(4,117,100,15);
    ECMlabel7->setGeometry(4,134,120,15);
    ECMlabel1->setStyleSheet(LabelStyle());
    ECMlabel2->setStyleSheet(LabelStyle());
    ECMlabel3->setStyleSheet(LabelStyle());
    ECMlabel4->setStyleSheet(LabelStyle());
    ECMlabel5->setStyleSheet(LabelStyle());
    ECMlabel6->setStyleSheet(LabelStyle());
    ECMlabel7->setStyleSheet(LabelStyle());
    QLineEdit *ECMedit1 = new QLineEdit(tr("75%"), ECMWidget);
    QLineEdit *ECMedit2 = new QLineEdit(tr("450 Pa"), ECMWidget);
    ECMedit1->setGeometry(130,117,70,15);
    ECMedit2->setGeometry(130,134,70,15);
    ECMedit1->setStyleSheet(LineEditStyle());
    ECMedit2->setStyleSheet(LineEditStyle());

    //Input
    sphereInputWidget = new QWidget(inputWidget);
    cylinderInputWidget = new QWidget(inputWidget);
    planeInputWidget = new QWidget(inputWidget);
    cubeInputWidget = new QWidget(inputWidget);

    QLabel *sname = new QLabel(tr("Sphere Input"), sphereInputWidget);
    sname->setGeometry(4,0,200,15);
    sname->setStyleSheet(LabelStyle());
    QLabel *sradius = new QLabel(tr("Radius:"), sphereInputWidget);
    QLabel *sheightSegmentation = new QLabel(tr("Height Segmentation:"), sphereInputWidget);
    QLabel *saxisSegmentation = new QLabel(tr("Axis Segmentation:"), sphereInputWidget);
    QLineEdit *sradiusEdit = new QLineEdit(tr("5.0"), sphereInputWidget);
    QLineEdit *sheightSegmentationEdit = new QLineEdit(tr("12"), sphereInputWidget);
    QLineEdit *saxisSegmentationEdit = new QLineEdit(tr("24"), sphereInputWidget);
    sradius->setGeometry(4,30,140,15);
    sheightSegmentation->setGeometry(4,47,140,15);
    saxisSegmentation->setGeometry(4,64,140,15);
    sradiusEdit->setGeometry(150,30,50,15);
    sheightSegmentationEdit->setGeometry(150,47,50,15);
    saxisSegmentationEdit->setGeometry(150,64,50,15);
    sradius->setStyleSheet(LabelStyle());
    sheightSegmentation->setStyleSheet(LabelStyle());
    saxisSegmentation->setStyleSheet(LabelStyle());
    sradiusEdit->setStyleSheet(LineEditStyle());
    sheightSegmentationEdit->setStyleSheet(LineEditStyle());
    saxisSegmentationEdit->setStyleSheet(LineEditStyle());
    connect(sradiusEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setSphereRadius(QString)));
    connect(sheightSegmentationEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setSphereHeightSeg(QString)));
    connect(saxisSegmentationEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setSphereAxisSeg(QString)));
    sphereInputWidget->setVisible(false);

    QLabel *cyname = new QLabel(tr("Cylinder Input"), cylinderInputWidget);
    cyname->setGeometry(4,0,200,15);
    cyname->setStyleSheet(LabelStyle());
    QLabel *cyradius = new QLabel(tr("Radius:"), cylinderInputWidget);
    QLabel *cyheight = new QLabel(tr("Height:"), cylinderInputWidget);
    QLabel *cyheightSegmentation = new QLabel(tr("Height Segmentation:"), cylinderInputWidget);
    QLabel *cyaxisSegmentation = new QLabel(tr("Axis Segmentation:"), cylinderInputWidget);
    QLineEdit *cyradiusEdit = new QLineEdit(tr("5.0"), cylinderInputWidget);
    QLineEdit *cyheightEdit = new QLineEdit(tr("5.0"), cylinderInputWidget);
    QLineEdit *cyheightSegmentationEdit = new QLineEdit(tr("1"), cylinderInputWidget);
    QLineEdit *cyaxisSegmentationEdit = new QLineEdit(tr("24"), cylinderInputWidget);
    cyradius->setGeometry(4,30,140,15);
    cyheight->setGeometry(4,47,140,15);
    cyheightSegmentation->setGeometry(4,64,140,15);
    cyaxisSegmentation->setGeometry(4,81,140,15);
    cyradiusEdit->setGeometry(150,30,50,15);
    cyheightEdit->setGeometry(150,47,50,15);
    cyheightSegmentationEdit->setGeometry(150,64,50,15);
    cyaxisSegmentationEdit->setGeometry(150,81,50,15);
    cyradius->setStyleSheet(LabelStyle());
    cyheight->setStyleSheet(LabelStyle());
    cyheightSegmentation->setStyleSheet(LabelStyle());
    cyaxisSegmentation->setStyleSheet(LabelStyle());
    cyradiusEdit->setStyleSheet(LineEditStyle());
    cyheightEdit->setStyleSheet(LineEditStyle());
    cyheightSegmentationEdit->setStyleSheet(LineEditStyle());
    cyaxisSegmentationEdit->setStyleSheet(LineEditStyle());
    connect(cyradiusEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCylinderRadius(QString)));
    connect(cyheightEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCylinderHeight(QString)));
    connect(cyheightSegmentationEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCylinderHeightSeg(QString)));
    connect(cyaxisSegmentationEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCylinderAxisSeg(QString)));
    cylinderInputWidget->setVisible(false);

    QLabel *pname = new QLabel(tr("Plane Input"), planeInputWidget);
    pname->setGeometry(4,0,200,15);
    pname->setStyleSheet(LabelStyle());
    QLabel *pwidth = new QLabel(tr("Width:"), planeInputWidget);
    QLabel *plength = new QLabel(tr("Length:"), planeInputWidget);
    QLabel *pSegmentation = new QLabel(tr("Segmentation:"), planeInputWidget);
    QLineEdit *pwidthEdit = new QLineEdit(tr("5.0"), planeInputWidget);
    QLineEdit *plengthEdit = new QLineEdit(tr("5.0"), planeInputWidget);
    QLineEdit *pSegmentationEdit = new QLineEdit(tr("1"), planeInputWidget);
    pwidth->setGeometry(4,30,140,15);
    plength->setGeometry(4,47,140,15);
    pSegmentation->setGeometry(4,64,140,15);
    pwidthEdit->setGeometry(150,30,50,15);
    plengthEdit->setGeometry(150,47,50,15);
    pSegmentationEdit->setGeometry(150,64,50,15);
    pwidth->setStyleSheet(LabelStyle());
    plength->setStyleSheet(LabelStyle());
    pSegmentation->setStyleSheet(LabelStyle());
    pwidthEdit->setStyleSheet(LineEditStyle());
    plengthEdit->setStyleSheet(LineEditStyle());
    pSegmentationEdit->setStyleSheet(LineEditStyle());
    connect(pwidthEdit, SIGNAL(textEdited(QString)),
            perspectiveWidget, SLOT(setPlaneWidth(QString)));
    connect(plengthEdit, SIGNAL(textEdited(QString)),
            perspectiveWidget, SLOT(setPlaneLength(QString)));
    connect(pSegmentationEdit, SIGNAL(textEdited(QString)),
            perspectiveWidget, SLOT(setPlaneSeg(QString)));
    planeInputWidget->setVisible(false);

    QLabel *cuname = new QLabel(tr("Cube Input"), cubeInputWidget);
    cuname->setGeometry(4,0,200,15);
    cuname->setStyleSheet(LabelStyle());
    QLabel *cuwidth = new QLabel(tr("Width:"), cubeInputWidget);
    QLabel *cuheight = new QLabel(tr("Height:"), cubeInputWidget);
    QLabel *cudepth = new QLabel(tr("Depth:"), cubeInputWidget);
    QLabel *cuwidthSegmentation = new QLabel(tr("Width Segmentation:"), cubeInputWidget);
    QLabel *cuheightSegmentation = new QLabel(tr("Height Segmentation:"), cubeInputWidget);
    QLabel *cudepthSegmentation = new QLabel(tr("Depth Segmentation:"), cubeInputWidget);
    QLineEdit *cuwidthEdit = new QLineEdit(tr("5.0"), cubeInputWidget);
    QLineEdit *cuheightEdit = new QLineEdit(tr("5.0"), cubeInputWidget);
    QLineEdit *cudepthEdit = new QLineEdit(tr("5.0"), cubeInputWidget);
    QLineEdit *cuwidthSegmentationEdit = new QLineEdit(tr("1"), cubeInputWidget);
    QLineEdit *cuheightSegmentationEdit = new QLineEdit(tr("1"), cubeInputWidget);
    QLineEdit *cudepthSegmentationEdit = new QLineEdit(tr("1"), cubeInputWidget);
    cuwidth->setGeometry(4,30,140,15);
    cuheight->setGeometry(4,47,140,15);
    cudepth->setGeometry(4,64,140,15);
    cuwidthSegmentation->setGeometry(4,81,140,15);
    cuheightSegmentation->setGeometry(4,98,140,15);
    cudepthSegmentation->setGeometry(4,115,140,15);
    cuwidthEdit->setGeometry(150,30,50,15);
    cuheightEdit->setGeometry(150,47,50,15);
    cudepthEdit->setGeometry(150,64,50,15);
    cuwidthSegmentationEdit->setGeometry(150,81,50,15);
    cuheightSegmentationEdit->setGeometry(150,98,50,15);
    cudepthSegmentationEdit->setGeometry(150,115,50,15);
    cuwidth->setStyleSheet(LabelStyle());
    cuheight->setStyleSheet(LabelStyle());
    cudepth->setStyleSheet(LabelStyle());
    cuwidthSegmentation->setStyleSheet(LabelStyle());
    cuheightSegmentation->setStyleSheet(LabelStyle());
    cudepthSegmentation->setStyleSheet(LabelStyle());
    cuwidthEdit->setStyleSheet(LineEditStyle());
    cuheightEdit->setStyleSheet(LineEditStyle());
    cudepthEdit->setStyleSheet(LineEditStyle());
    cuwidthSegmentationEdit->setStyleSheet(LineEditStyle());
    cuheightSegmentationEdit->setStyleSheet(LineEditStyle());
    cudepthSegmentationEdit->setStyleSheet(LineEditStyle());
    connect(cuwidthEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCubeWidth(QString)));
    connect(cuheightEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCubeHeight(QString)));
    connect(cudepthEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCubeDepth(QString)));
    connect(cuwidthSegmentationEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCubeWidthSeg(QString)));
    connect(cuheightSegmentationEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCubeHeightSeg(QString)));
    connect(cudepthSegmentationEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setCubeDepthSeg(QString)));
    cubeInputWidget->setVisible(false);

    connect(perspectiveWidget, SIGNAL(inputTypeChanged(int)),
            this, SLOT(inputSlot(int)));

    objectToolBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored));
    objectToolBox->setMinimumSize(200,370); //How large a tab opens up
    objectToolBox->addItem(cellWidget, tr("Cell"));
    objectToolBox->addItem(basementMembraneWidget, tr("Basement Membrane"));
    objectToolBox->addItem(bloodVesselWidget, tr("Blood Vessel"));
    objectToolBox->addItem(ECMWidget, tr("ECM"));
    objectToolBox->addItem(inputWidget, tr("Input"));
    objectToolBox->setStyleSheet(ToolBoxStyle());
    objectToolWidget->setGeometry(0,0,200,500); //allotted space for the tab in the rightPart
}



void CellMainWindow::createImformationWidget()
{
    imformationWidget = new QWidget(rightPart);
    objectNameLabel = new QLabel(tr("Object Name"), imformationWidget);
    translateXLabel = new QLabel(tr("TranslateX:"), imformationWidget);
    translateYLabel = new QLabel(tr("TranslateY:"), imformationWidget);
    translateZLabel = new QLabel(tr("TranslateZ:"), imformationWidget);
    rotateXLabel = new QLabel(tr("RotateX:"), imformationWidget);
    rotateYLabel = new QLabel(tr("RotateY:"), imformationWidget);
    rotateZLabel = new QLabel(tr("RotateZ:"), imformationWidget);
    scaleXLabel = new QLabel(tr("ScaleX:"), imformationWidget);
    scaleYLabel = new QLabel(tr("ScaleY:"), imformationWidget);
    scaleZLabel = new QLabel(tr("ScaleZ:"), imformationWidget);
    objectNameLabel->setGeometry(0,0,100,18);
    translateXLabel->setGeometry(30,21,100,18);
    translateYLabel->setGeometry(30,42,100,18);
    translateZLabel->setGeometry(30,63,100,18);
    rotateXLabel->setGeometry(46,84,100,18);
    rotateYLabel->setGeometry(46,105,100,18);
    rotateZLabel->setGeometry(46,126,100,18);
    scaleXLabel->setGeometry(54,147,100,18);
    scaleYLabel->setGeometry(54,168,100,18);
    scaleZLabel->setGeometry(54,189,100,18);
    objectNameLabel->setStyleSheet(LabelStyle());
    translateXLabel->setStyleSheet(LabelStyle());
    translateYLabel->setStyleSheet(LabelStyle());
    translateZLabel->setStyleSheet(LabelStyle());
    rotateXLabel->setStyleSheet(LabelStyle());
    rotateYLabel->setStyleSheet(LabelStyle());
    rotateZLabel->setStyleSheet(LabelStyle());
    scaleXLabel->setStyleSheet(LabelStyle());
    scaleYLabel->setStyleSheet(LabelStyle());
    scaleZLabel->setStyleSheet(LabelStyle());

    translateXEdit = new QLineEdit(tr("0"), imformationWidget);
    connect(translateXEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setTranslateX(QString)));
    connect(perspectiveWidget, SIGNAL(xTranslationChanged(QString)),
            translateXEdit, SLOT(setText(QString)));

    translateYEdit = new QLineEdit(tr("0"), imformationWidget);
    connect(translateYEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setTranslateY(QString)));
    connect(perspectiveWidget, SIGNAL(yTranslationChanged(QString)),
            translateYEdit, SLOT(setText(QString)));

    translateZEdit = new QLineEdit(tr("0"), imformationWidget);
    connect(translateZEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setTranslateZ(QString)));
    connect(perspectiveWidget, SIGNAL(zTranslationChanged(QString)),
            translateZEdit, SLOT(setText(QString)));

    rotateXEdit = new QLineEdit(tr("0"), imformationWidget);
    connect(rotateXEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setRotateX(QString)));
    connect(perspectiveWidget, SIGNAL(xRotationChanged(QString)),
            rotateXEdit, SLOT(setText(QString)));

    rotateYEdit = new QLineEdit(tr("0"), imformationWidget);
    connect(rotateYEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setRotateY(QString)));
    connect(perspectiveWidget, SIGNAL(yRotationChanged(QString)),
            rotateYEdit, SLOT(setText(QString)));

    rotateZEdit = new QLineEdit(tr("0"), imformationWidget);
    connect(rotateZEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setRotateZ(QString)));
    connect(perspectiveWidget, SIGNAL(zRotationChanged(QString)),
            rotateZEdit, SLOT(setText(QString)));

    scaleXEdit = new QLineEdit(tr("1"), imformationWidget);
    connect(scaleXEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setScaleX(QString)));
    connect(perspectiveWidget, SIGNAL(xScalationChanged(QString)),
            scaleXEdit, SLOT(setText(QString)));

    scaleYEdit = new QLineEdit(tr("1"), imformationWidget);
    connect(scaleYEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setScaleY(QString)));
    connect(perspectiveWidget, SIGNAL(yScalationChanged(QString)),
            scaleYEdit, SLOT(setText(QString)));

    scaleZEdit = new QLineEdit(tr("1"), imformationWidget);
    connect(scaleZEdit, SIGNAL(textEdited(QString)),
            this, SLOT(setScaleZ(QString)));
    connect(perspectiveWidget, SIGNAL(zScalationChanged(QString)),
            scaleZEdit, SLOT(setText(QString)));
//    translateXEdit->setEnabled(false);
//    translateYEdit->setEnabled(false);
//    translateZEdit->setEnabled(false);
//    rotateXEdit->setEnabled(false);
//    rotateYEdit->setEnabled(false);
//    rotateZEdit->setEnabled(false);
//    scaleXEdit->setEnabled(false);
//    scaleYEdit->setEnabled(false);
//    scaleZEdit->setEnabled(false);
    translateXEdit->setStyleSheet(LineEditStyle());
    translateYEdit->setStyleSheet(LineEditStyle());
    translateZEdit->setStyleSheet(LineEditStyle());
    rotateXEdit->setStyleSheet(LineEditStyle());
    rotateYEdit->setStyleSheet(LineEditStyle());
    rotateZEdit->setStyleSheet(LineEditStyle());
    scaleXEdit->setStyleSheet(LineEditStyle());
    scaleYEdit->setStyleSheet(LineEditStyle());
    scaleZEdit->setStyleSheet(LineEditStyle());
    translateXEdit->setGeometry(100,21,100,18);
    translateYEdit->setGeometry(100,42,100,18);
    translateZEdit->setGeometry(100,63,100,18);
    rotateXEdit->setGeometry(100,84,100,18);
    rotateYEdit->setGeometry(100,105,100,18);
    rotateZEdit->setGeometry(100,126,100,18);
    scaleXEdit->setGeometry(100,147,100,18);
    scaleYEdit->setGeometry(100,168,100,18);
    scaleZEdit->setGeometry(100,189,100,18);
    imformationWidget->setGeometry(0,390,200,207);
}

void CellMainWindow::meshEditModeGroupClicked(QAbstractButton *button)
{
    QList<QAbstractButton *> buttons = transformToolGroup->buttons();
    foreach (QAbstractButton *myButton, buttons) {
        if (myButton != button)
        button->setChecked(false);
    }
    QString text = button->objectName();
    if (text == tr("Point")) {
        perspectiveWidget->setSelectMode(CELL_EDITMESHPOINT);
    } else if (text == tr("Triangle")) {
        perspectiveWidget->setSelectMode(CELL_EDITMESHFACE);
    }

    perspectiveWidget->updateGL();
}

void CellMainWindow::deleteButtonPressed()
{
    perspectiveWidget->deleteSelect();
}

void CellMainWindow::selectClicked(QListWidgetItem *current, QListWidgetItem *previous)
{
    if (!current)
        current = previous;

    string text = current->text().toStdString();
    if (text==string("none")) {
        //cout << "cellmainwindow1: " << perspectiveWidget->getSelectObject() << endl;
        perspectiveWidget->setSelectObject(-1);
        //cout << "cellmainwindow1: " << perspectiveWidget->getSelectObject() << endl;
    } else if (text==perspectiveWidget->getGlandular()->getName()) {
        //cout << "cellmainwindow2: " << perspectiveWidget->getSelectObject() << endl;
        perspectiveWidget->setSelectObject(0);
        //cout << "cellmainwindow2: " << perspectiveWidget->getSelectObject() << endl;
    } else {
        for (size_t i=0;i<perspectiveWidget->getObjects().size();i++) {
            if (text==perspectiveWidget->getObjects().at(i)->getName()) {
                //cout << "cellmainwindow3: " << perspectiveWidget->getSelectObject() << endl;
                perspectiveWidget->setSelectObject(i+2);
                //cout << "cellmainwindow3: " << perspectiveWidget->getSelectObject() << endl;
                break;
            }
        }
    }
    perspectiveWidget->updateGL();
}

void CellMainWindow::createSelectWidget()
{
    selectDockWidget = new QDockWidget;
    addDockWidget(Qt::RightDockWidgetArea, selectDockWidget);
    selectDockWidget->setFloating(true);
    selectDockWidget->setMinimumSize(200,500);
    selectDockWidget->setAllowedAreas(Qt::NoDockWidgetArea);
    selectDockWidget->setWindowTitle(tr("Select Object"));
    selectDockWidget->setStyleSheet(DockWidgetStyle());
    selectDockWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
    selectDockWidget->setFeatures(QDockWidget::DockWidgetClosable);
    selectDockWidget->setFeatures(QDockWidget::DockWidgetFloatable);

    selectWidget = new QWidget;
    selectListWidget = new QListWidget;


    new QListWidgetItem(tr("none"), selectListWidget);
    if (perspectiveWidget->getGlandular()->getVerticeNum()>0) {
        new QListWidgetItem(tr(perspectiveWidget->getGlandular()->getName().c_str()), selectListWidget);
    }
    for (size_t i=0;i<perspectiveWidget->getObjects().size();i++) {
        new QListWidgetItem(tr(perspectiveWidget->getObjects().at(i)->getName().c_str()), selectListWidget);
    }
    connect(selectListWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
                this, SLOT(selectClicked(QListWidgetItem*,QListWidgetItem*)));
    if (perspectiveWidget->selectedObject()<0)
        selectListWidget->setCurrentRow(0);
    else if (perspectiveWidget->selectedObject()==0)
        selectListWidget->setCurrentRow(1);
    else if (perspectiveWidget->selectedObject()>1) {
        if (perspectiveWidget->getGlandular()->getVerticeNum()>0) {
            selectListWidget->setCurrentRow(perspectiveWidget->selectedObject());
        } else {
            selectListWidget->setCurrentRow(perspectiveWidget->selectedObject()-1);
        }
    }

    selectDockWidget->setWidget(selectListWidget);
}

void CellMainWindow::createMeshEditWidget()
{
    //Create the QDockWidget
    editMeshDockWidget = new QDockWidget;
    addDockWidget(Qt::RightDockWidgetArea, editMeshDockWidget);
    editMeshDockWidget->setFloating(false);
    editMeshDockWidget->setMinimumSize(200,500);
    editMeshDockWidget->setAllowedAreas(Qt::RightDockWidgetArea);
    editMeshDockWidget->setWindowTitle(tr("Mesh Edit"));
    editMeshDockWidget->setStyleSheet(DockWidgetStyle());
    editMeshDockWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);
    editMeshDockWidget->setFeatures(QDockWidget::DockWidgetClosable);
    editMeshDockWidget->installEventFilter(this);

    editMeshWidget = new QWidget;
    meshEditModeGroup = new QButtonGroup(this);
    connect(meshEditModeGroup, SIGNAL(buttonClicked(QAbstractButton*)),
                this, SLOT(meshEditModeGroupClicked(QAbstractButton*)));
    createToolButton(editMeshWidget, meshEditModeGroup, pointsEditButton,
                     tr("Point"), tr(":/images/point"), 20, 20, 40, 40, false);
    createToolButton(editMeshWidget, meshEditModeGroup, facesEditButton,
                     tr("Triangle"), tr(":/images/triangle"), 80, 20, 40, 40, false);
    QLabel *featherLabel = new QLabel(tr("Feather:"), editMeshWidget);
    featherLabel->setGeometry(20, 80, 100, 15);
    featherLabel->setStyleSheet(LabelStyle());
    QLineEdit *featherEdit = new QLineEdit(tr("0"), editMeshWidget);
    featherEdit->setGeometry(80,80,50,15);
    featherEdit->setStyleSheet(LineEditStyle());
    perspectiveWidget->setEditFeather(0.0);
    connect(featherEdit, SIGNAL(textEdited(QString)),
            perspectiveWidget, SLOT(setEditFeather(QString)));
    QLabel *featherLabel2 = new QLabel(tr("%"), editMeshWidget);
    featherLabel2->setGeometry(135, 80, 10, 15);
    featherLabel2->setStyleSheet(LabelStyle());
    QPushButton *deleteButton = new QPushButton(editMeshWidget);
    connect(deleteButton, SIGNAL(pressed()), this, SLOT(deleteButtonPressed()));
    deleteButton->setGeometry(20, 110, 60, 20);
    deleteButton->setStyleSheet(PushButtonStyle(tr(":/images/delete")));
    deleteButton->setObjectName(tr("Delete"));
    deleteButton->setText(tr("Delete"));
    deleteButton->setFixedSize(60, 20);
    deleteButton->setCheckable(true);
    deleteButton->setStatusTip(tr("Delete"));
    editMeshDockWidget->setWidget(editMeshWidget);
}

void CellMainWindow::rightClickMenuSlot(QPoint pos)
{
    rightClickMenu->popup(pos);
}

void CellMainWindow::deleteSelectedObjectSlot()
{
    /*
    //if an object is selected properly
    if(perspectiveWidget->selectedObject() >= 2)
    {
        QUndoCommand *deleteObjectCommand = new DeleteObjectCommand(perspectiveWidget, perspectiveWidget->selectedObject());
        undoStack->push(deleteObjectCommand);
    }
    */
    perspectiveWidget->deleteSelectedObject();
    perspectiveWidget->updateGL();
}

void CellMainWindow::generateMeshSlot()
{
    perspectiveWidget->generateMesh();
    perspectiveWidget->updateGL();
}

void CellMainWindow::newScene()
{
    perspectiveWidget->newScene();
    perspectiveWidget->updateGL();
}

void CellMainWindow::importXML()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                    tr("Open XML file"), "",
                                    tr("XML Files (*.xml);;"
                                       "All Files (*)"));
    TiXmlDocument *myDocument = new TiXmlDocument(fileName.toLatin1().data());
    statusBar()->showMessage(tr("Loading File")+fileName, 2000);
    bool loadOkay = myDocument->LoadFile();
    if (loadOkay) {
        double x, y, z;
        int node_num = 0;
        vector<double> vertice;
        vector<double> normals;
        TiXmlElement *RootElement = myDocument->RootElement();
        TiXmlElement *firstNode = RootElement->FirstChildElement()->FirstChildElement()->FirstChildElement();
        while (firstNode) {
            TiXmlElement *verticeElement = firstNode->FirstChildElement();
            TiXmlElement *normalElement = verticeElement->NextSiblingElement();
            sscanf(verticeElement->FirstChild()->Value(),"(%lf,%lf,%lf)",&x,&y,&z);
            vertice.push_back(x);
            vertice.push_back(y);
            vertice.push_back(z);
            sscanf(normalElement->FirstChild()->Value(),"(%lf,%lf,%lf)",&x,&y,&z);
            normals.push_back(x);
            normals.push_back(y);
            normals.push_back(z);
            node_num++;
            firstNode = firstNode->NextSiblingElement();
        }
        //Output .obj file.
//        FILE *out;
//        out = fopen("mesh.obj", "w+");
//        for (int i=0;i<node_num;i++)
//            fprintf(out, "v %lf, %lf, %lf\n", vertice[i*3+0], vertice[i*3+1], vertice[i*3+2]);
//        for (int i=0;i<node_num;i++)
//            fprintf(out, "vn %lf, %lf, %lf\n", normals[i*3+0], normals[i*3+1], normals[i*3+2]);

        perspectiveWidget->addGlandular(node_num, vertice, normals);
        perspectiveWidget->updateGL();
    }
}

void CellMainWindow::importSBML()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                    tr("Open XML file"), "",
                                    tr("XML Files (*.sbml);;"
                                       "All Files (*)"));
}

void CellMainWindow::save()
{
    QString fileName = QFileDialog::getSaveFileName(this,
                                                    tr("Save File"),"",
                                                    tr("XML Files (*.xml);;"
                                                       "All Files (*)"));
    //perspectiveWidget->getGlandular()->SaveCellObject(fileName.toLatin1().data());
    perspectiveWidget->SaveScene(fileName.toLatin1().data());
}

void CellMainWindow::cut()
{

}

void CellMainWindow::copy()
{
    perspectiveWidget->setCopy(perspectiveWidget->selectedObject());
    perspectiveWidget->updateGL();
}

void CellMainWindow::paste()
{
    perspectiveWidget->pasteObject();
    perspectiveWidget->updateGL();
}

void CellMainWindow::select()
{
    createSelectWidget();
    perspectiveWidget->updateGL();
}

